package edu.hawaii.duedates;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Test case for DueDates class.
 * 
 * @author Yasuyuki Kaneshige and Jeho Jung
 */

public class TestDueDates {
  
  /**
   * Tests for no books checked out.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testNoBooksOut() throws Exception {
    String id = "16047587";
    String lastName = "jung";
    DueDates dueDate = new DueDates();
    String uhInfo = dueDate.getUhInfo(id, lastName);
    assertEquals("Testing no books out.", "No items checked out", uhInfo);
  }

  
  /**
   * Tests for books checked out.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testBooksOut() throws Exception {
    DueDates dueDate = new DueDates();
    String book01 = "Scott, Jill, 1972-  Beautiful";
    String id = "11418234";
    String lastName = "kaneshige";
    try {
      WebConversation conversation = dueDate.loginToUhLibrary(id, lastName);
      WebResponse response = conversation.getCurrentPage();
      WebTable table = response.getTables()[0];
      assertEquals("Testing books out.", book01, table.getCellAsText(1, 1).substring(0, 29));
    }
    catch (Exception e) {
      throw new Exception("Error getting UH Book info", e);
    }  
  }

  
  /**
   * Tests if loginToUhLibrary works.
   * 
   * @exception Exception if login errors.
   */
  @Test
  public void testLogin() throws Exception {
    String id = "11418234";
    String lastName = "kaneshige";
    DueDates dueDate = new DueDates();
    WebConversation conversation = dueDate.loginToUhLibrary(id, lastName);
    WebResponse response = conversation.getCurrentPage();
    assertTrue("Testing correct Login Display", response.getText().contains("Checked Out Items"));
  }
}
